package com.book.dao;

import java.sql.Connection;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.book.entity.Book;
import com.book.util.BaseDao;

public class ManagerDao {
	//查询图书
	public List<Book> findBookAll(){
		try{
			List<Book> list=new ArrayList<Book>();
		Connection conn=BaseDao.getConnection();
		PreparedStatement ps=conn.prepareStatement("select * from book");
		 ResultSet rs=ps.executeQuery();
		 Book b=null;
		 while(rs.next()){
			 b=new Book();
			 b.setBook_id(rs.getString("book_id"));
			 b.setBook_name(rs.getString("book_name"));
			 b.setAuthor(rs.getString("author"));
			 b.setPress(rs.getString("press"));
		     list.add(b);
			 System.out.println(b);
		 }
		 return list;
		 
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return null;
	}
		
	}
	
	
	//添加图书
	public boolean addBook(Book b){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("insert into book(book_id,book_name,author,press) values(?,?,?,?)");
			pstm.setString(1, b.getBook_id());
			pstm.setString(2, b.getBook_name());
			pstm.setString(3, b.getAuthor());
			pstm.setString(4, b.getPress());
		
			int rs = pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	//修改图书
	public boolean updateBook(Book book){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("update book set book_id=?,book_name=?,author=?,press=? where book_id=?");
			pstm.setString(1, book.getBook_id());

			pstm.setString(2,  book.getBook_name());
			pstm.setString(3, book.getAuthor());
			pstm.setString(4, book.getPress());
			pstm.setString(5, book.getBook_id());
            int rs = pstm.executeUpdate();
            System.out.println(rs);
            if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	//删除图书
	public boolean deleteBookByid(String book_id){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("delete from book where book_id=?");
			pstm.setString(1, book_id);
			int rs=pstm.executeUpdate();
			if(rs>0){
				return true;
			}else{
				return false;
			}
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}
	}
	/*
	 * 验证该商品是否存在<根据id>
	 */
	public boolean isExistId(String book_id){
		return findBookById(book_id)==null?false:true;
	}
	/*
	 * 验证该商品是否存在<根据name>
	 */
	public boolean isExist(String name){
		return findBookByName(name)==null?false:true;
	}
	/*
	 * 根据id查询相应商品
	 */
	public Book findBookById(String book_id){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("select * from book where book_id=?");
			//pstm.set(1, book_id);
			pstm.setString(1, book_id);
			ResultSet rs=pstm.executeQuery();
			Book b=null;
			while(rs.next()){
				b=new Book();
				b.setBook_id(rs.getString(1));
				b.setBook_name(rs.getString(2));
				b.setAuthor(rs.getString(3));
				b.setPress(rs.getString(4));
				
				
			}
			return b;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	/*
	 * 根据name查询相应商品
	 */
	public Book findBookByName(String name){
		try{
			Connection con=BaseDao.getConnection();
			PreparedStatement pstm=con.prepareStatement("select * from book where book_name=?");
			pstm.setString(1, name);
			ResultSet rs=pstm.executeQuery();
			Book b=null;
			while(rs.next()){
				b=new Book();
				b.setBook_id(rs.getString(1));
				b.setBook_name(rs.getString(2));
				b.setAuthor(rs.getString(3));
				b.setPress(rs.getString(4));
			}
			return b;
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}


}
